package com.ruoyi.information.controller.pc;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.information.domain.pc.SysNotice;
import com.ruoyi.information.service.pc.ISysNoticeService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 系统通知Controller
 *
 * @author ruoyi
 * @date 2023-05-22
 */
@RestController
@RequestMapping("/notice")
public class SysNoticeController extends BaseController {
    @Autowired
    private ISysNoticeService sysNoticeService;

    /**
     * 查询系统通知列表(未登录)
     */
    @GetMapping("/list")
    public AjaxResult list(SysNotice sysNotice) {
        AjaxResult ajaxResult = new AjaxResult();
//        List<SysNotice> list = sysNoticeService.selectSysNoticeList(sysNotice);
        ajaxResult.put("rows", null);
        ajaxResult.put("code", 200);
        ajaxResult.put("msg", "查询成功");
        return ajaxResult;
    }

    /**
     * 查询系统通知列表(已登录)
     */
    @GetMapping("/loginList")
    public AjaxResult loginList(SysNotice sysNotice) {
        AjaxResult ajaxResult = new AjaxResult();
        List<SysNotice> list = sysNoticeService.selectSysNoticeLoginList(sysNotice);
        ajaxResult.put("rows", list);
        ajaxResult.put("code", 200);
        ajaxResult.put("msg", "查询成功");
        return ajaxResult;
    }

    /**
     * 查询系统通知列表(已登录)
     */
    @GetMapping("/loginListX")
    public AjaxResult loginListX(SysNotice sysNotice, @RequestParam(value = "pageSize", defaultValue = "1") Integer pageSize
            , @RequestParam(value = "pageNum", defaultValue = "10") Integer pageNum) {
        AjaxResult ajaxResult = new AjaxResult();
        List<SysNotice> list = sysNoticeService.selectSysNoticeLoginList(sysNotice);
        if (list != null && !list.isEmpty()) {
            list = list.stream().sorted(Comparator.comparing(SysNotice::getCreateTime).reversed()).skip((long) (pageNum - 1) * pageSize).limit(pageSize).
                    collect(Collectors.toList());
            ajaxResult.put("rows", list);
            ajaxResult.put("total", list.size());
        } else {
            ajaxResult.put("rows", null);
            ajaxResult.put("total", 0);
        }
        ajaxResult.put("code", 200);
        ajaxResult.put("msg", "查询成功");
        return ajaxResult;
    }

    /**
     * 查询系统通知列表
     */
    @GetMapping("/app")
    public AjaxResult app(SysNotice sysNotice) {
        startPage();
        List<SysNotice> list = sysNoticeService.selectSysNoticeList(sysNotice);
        return success(list);
    }


    /**
     * 获取系统通知详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sysNoticeService.selectSysNoticeById(id, false));
    }


    /**
     * 获取系统通知详细信息
     */
    @GetMapping(value = "/getOne")
    public AjaxResult getOne(@Param("id") Long id) {
        return success(sysNoticeService.selectSysNoticeById(id, false));
    }

    /**
     * 获取系统通知详细信息
     */
    @GetMapping(value = "/insertLookUser")
    public AjaxResult insertLookUser(@Param("id") Long id) {
        return success(sysNoticeService.selectSysNoticeById(id, true));
    }

    /**
     * 新增系统通知
     */
    @PostMapping
    public AjaxResult add(@RequestBody SysNotice sysNotice) {
        return toAjax(sysNoticeService.insertSysNotice(sysNotice));
    }

    /**
     * 修改系统通知
     */
    @PutMapping
    public AjaxResult edit(@RequestBody SysNotice sysNotice) {
        return toAjax(sysNoticeService.updateSysNotice(sysNotice));
    }

    /**
     * 删除系统通知
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysNoticeService.deleteSysNoticeByIds(ids));
    }
}
